﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class Books : PageBase
    {
        string Key = string.Empty;
        string ChannelId = string.Empty;
        string IsHid = "-1";
        BLL.Books bll = new BLL.Books();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("shopping", "read");

            if (!string.IsNullOrEmpty(Request.QueryString["key"]))
            {
                Key = Request.QueryString["key"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["channelid"]))
            {
                ChannelId = Request.QueryString["channelid"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["hid"]))
            {
                IsHid = Request.QueryString["hid"];
            }

            if (!IsPostBack)
            {
                LoadList();
                ddlChannel.Items.Insert(0, new ListItem("请选择", "0"));

                BindSearch();
                BindData();
            }
        }

        BLL.Channel chlBLL = new BLL.Channel();
        /// <summary>
        /// 加载整个分类表
        /// </summary>
        private void LoadList()
        {
            int MTcount = 0;//执行遍历的次数
            //开始迭代加载
            MakeTr("0", MTcount);

        }
        /// <summary>
        /// 装配每一行
        /// </summary>
        /// <param name="id">上级ID</param>
        /// <param name="count">迭代次数</param>
        private void MakeTr(string id, int count)
        {
            DataView dv = new DataView(chlBLL.GetList("parentID=" + id + " order by ChannelId asc").Tables[0]);
            foreach (DataRowView drv in dv)
            {
                //形成下拉列表
                ListItem li = new ListItem(MakeFelgefu(count) + drv["ChannelName"].ToString(), drv["ChannelId"].ToString());
                ddlChannel.Items.Add(li);

                //再次遍历
                MakeTr(drv["ChannelId"].ToString(), count + 1);
            }

        }
        /// <summary>
        /// Make分隔符
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string MakeFelgefu(int count)
        {
            string Returnwords = string.Empty;
            if (count == 0)
            {
                Returnwords = "";
            }

            else
            {
                Returnwords = ("┠").PadLeft(count, '　');
            }
            return Returnwords;
        }

        protected void BindSearch()
        {
            if (!string.IsNullOrEmpty(Key))
            {
                txtKey.Value = Key;
            }
            if (!string.IsNullOrEmpty(ChannelId))
            {
                ddlChannel.SelectedValue = ChannelId;
            }
            if (!string.IsNullOrEmpty(IsHid))
            {
                ddlHidden.SelectedValue = IsHid;
            }
        }

        protected void BindData()
        {
            string strWhere = "IsActive=1";
            if (!string.IsNullOrEmpty(ChannelId) && ChannelId != "0")
            {
                strWhere += " and CategoryId=" + ChannelId;
            }
            if (!string.IsNullOrEmpty(Key))
            {
                strWhere += " and (bookName like '%" + Key + "%' or ISBN like '%" + Key + "%')";
            }
            if (!string.IsNullOrEmpty(IsHid) && IsHid != "-1")
            {
                strWhere += " and isnull(isHid,0)=" + IsHid;
            }
            //strWhere += " order by BookId desc";
            int recordCount = bll.GetRecordCount(strWhere);
            AspNetPager1.RecordCount = recordCount;
            DataSet ds = bll.GetListByPage(strWhere, "bookId desc", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected string GetChannelName(string cid)
        {
            BLL.Channel bllChannel = new BLL.Channel();
            return bllChannel.GetModel(int.Parse(cid)).ChannelName;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Redirect();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Check("shopping", "del");

            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                bll.DeleteByIds(ids);
            }

            Redirect();
        }

        /// <summary>
        /// 跳转页面
        /// </summary>
        private void Redirect()
        {
            Response.Redirect("Books.aspx?channelId=" + ddlChannel.SelectedValue + "&hid=" + ddlHidden.SelectedValue + "&key=" + txtKey.Value.Trim());
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            //string ids = txtIds.Value;
            //string categoryId = ddlType1.SelectedValue;
            //if (!string.IsNullOrEmpty(categoryId))
            //{
            //    bll.ModifySalePriceByIds(categoryId, txtDiscount.Text.Trim(), "1");
            //}
            //else if (!string.IsNullOrWhiteSpace(ids))
            //{
            //    bll.ModifySalePriceByIds(ids, txtDiscount.Text.Trim(), "0");
            //}
            MessageBox.ShowAndRedirect(this.Page, "修改成功", "Books.aspx?channelId=" + ddlChannel.SelectedValue + "&key=" + txtKey.Value.Trim());
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                bll.HiddenShow(ids, 1);
            }

            Redirect();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {

            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                bll.HiddenShow(ids, 0);
            }

            Redirect();
        }

        protected bool HasBookComment(string bookId)
        {
            //if (!string.IsNullOrEmpty(bookId))
            //{
            //    BLL.BookComments bcBll = new BLL.BookComments();
            //    DataSet ds = bcBll.GetList("bookId=" + bookId + " and IsActive=1");
            //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    {
            //        return true;
            //    }
            //}
            //return false;

            return false;
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }
    }
}